#include "modeselectwidget.h"
#include "ui_modeselectwidget.h"
#include <QDebug>
#include <QSpacerItem>
#include <iostream>


SelectItem::SelectItem(QString iconPath, QWidget *parent) :
    QPushButton(parent)
{
    m_icon = new QIcon(iconPath);
    this->setIcon(*m_icon);
    this->setIconSize(this->size() - QSize(10, 10));
    this->setCheckable(true);
    this->setText("");
    m_cornerRadius = this->size().width() / 2;
    connect(this, &QPushButton::clicked, this, [=]() {
       if (m_selected == false) {
           m_selected = true;
           emit selected(this);
       }
       else if (m_selected == true) {
           this->setChecked(true);
       }
    });
}

void SelectItem::select()
{
    this->setChecked(true);
    m_selected = true;
    emit selected(this);
}

void SelectItem::deSelect()
{
    this->setChecked(false);
    m_selected = false;
}


void SelectItem::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    if (this->size().width() > 10 && this->size().height() > 10)
        this->setIconSize(this->size() - QSize(10, 10));
    m_cornerRadius = std::min(this->size().width(), this->size().height()) / 2;
    //设置样式表
    QString style = QString("QPushButton {background-color:") +
            QString("rgba(174, 214, 241, 0)") +
            QString::asprintf(";border-radius:%dpx;}", m_cornerRadius) +
            QString("QPushButton:hover{background-color: ") +
            m_hoverColor.name() + QString::asprintf(";border-radius:%dpx;}", m_cornerRadius) +
            QString("QPushButton:unchecked{background-color: ") +
            m_uncheckedColor.name() + QString::asprintf(";border-radius:%dpx;}", m_cornerRadius) +
            QString("QPushButton:checked{background-color: ") +
            m_checkedColor.name() + QString::asprintf(";border-radius:%dpx;}", m_cornerRadius);
    this->setStyleSheet(style);
}

ModeSelectWidget::ModeSelectWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModeSelectWidget)
{
    m_orientation = vertical;
    this->m_layout = new QVBoxLayout(this);
    this->setLayout(m_layout);
    this->setMinimumWidth(50);
}

ModeSelectWidget::ModeSelectWidget(ModeSelectWidget::Orientation orientation, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModeSelectWidget),
    m_orientation(orientation)
{
    if (orientation == Orientation::vertical) {
        this->m_layout = new QVBoxLayout(this);
    }
    else
        this->m_layout = new QHBoxLayout(this);
    this->setLayout(m_layout);
    if (m_orientation == vertical) {
        this->setMinimumWidth(30);
        this->setMaximumWidth(50);
    }
    else {
        this->setMinimumHeight(30);
        this->setMaximumHeight(60);
    }
    //测试
    this->resize(100, 200);
    SelectItem *item1 = new SelectItem(":/images/image/icon/video1.png", this);
    SelectItem *item2 = new SelectItem(":/images/image/icon/image1.png", this);
    SelectItem *item3 = new SelectItem(":/images/image/icon/camera1.png", this);
    this->addItem(item1);
    this->addItem(item2);
    this->addItem(item3);
}

ModeSelectWidget::~ModeSelectWidget()
{
    for (int i = 0; i < m_itemVector.size(); i++) {
        delete m_itemVector[i];
    }
    delete m_layout;
    delete ui;
}

void ModeSelectWidget::addItem(SelectItem *item)
{
    m_itemVector.push_back(item);
    m_layout->addWidget(item);
    item->setMinimumSize(m_itemNormalSize);
    if (m_selectID == -1) {
        m_itemVector[0]->select();
        m_selectID = 0;
        qDebug() << "select : " << 0;
    }
    //按钮点击绑定
    connect(item, &SelectItem::selected, this, &ModeSelectWidget::changeSelection);
}


void ModeSelectWidget::changeSelection(SelectItem *item)
{
    int id = m_itemVector.indexOf(item);
    qDebug() << "select : " << id;
    for (int i = 0; i < m_itemVector.size(); i++) {
        if (i == id) continue;
        m_itemVector[i]->deSelect();
    }
    m_selectID = id;
    emit selectedItemChange(id);
}


